# -*- coding:UTF-8 -*-


"""
用于检验json数据是否符合标准
"""

from django.http import Http404
from utils.exceptions import InvalidInputException

def validate(schema,json_data):
    intersection_list = set(json_data.keys()) & set(schema.keys())
    for intersection in intersection_list:
        # 判断数据类型
        if not isinstance(json_data[intersection], schema[intersection]["type"]):
            raise InvalidInputException("%s数据类型错误" % json_data[intersection])
    required_data_list = []
    for data in schema.keys():
        if schema[data]["required"]:
            required_data_list.append(data)
    if set(required_data_list) <= set(json_data.keys()):
        print("参数正确")
        return True
    else:
        # 交集
        required_intersection = set(required_data_list) & set(json_data.keys())
        # 差
        diffrences = set(required_data_list) - required_intersection
        for diffrence in diffrences:
            ret_dict = {"success": False, "data": {"msg": "%s is a required parameter. " % diffrence}}
            raise InvalidInputException(ret_dict)

